<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      ref="dataForm"
      size="mini"
      :model="dataForm"
      :rules="dataRule"
      label-width="150px"
    >
      <el-row
        type="flex"
        class="row-bg"
      >
        <el-col :span="12">
          <el-form-item
            label="用户姓名"
            prop="userName"
          >
            <el-input
              v-model="dataForm.userName"
              clearable
              placeholder="用户姓名"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="用户帐号"
            prop="account"
          >
            <el-input
              v-model="dataForm.account"
              clearable
              placeholder="用户帐号"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row
        type="flex"
        class="row-bg"
      >
        <el-col :span="12">
          <!-- :class="{ 'is-required': !dataForm.userId }" -->
          <el-form-item
            label="密码"
            prop="password"
          >
            <el-input
              v-model="dataForm.password"
              clearable
              type="password"
              placeholder="密码"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- :class="{ 'is-required': !dataForm.userId }" -->
          <el-form-item
            label="确认密码"
            prop="comfirmPassword"
          >
            <el-input
              v-model="dataForm.comfirmPassword"
              clearable
              type="password"
              placeholder="确认密码"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row type="flex" class="row-bg">
        <el-col :span="12">
          <el-form-item label="业务单元" prop="deptId">
            <commonSelect :typeCode="'DEPT_NAME'" v-model="dataForm.deptId"></commonSelect>
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-row
        type="flex"
        class="row-bg"
      >
        <el-col :span="12">
          <el-form-item
            label="数据权限"
            prop="permissionType"
          >
            <commonSelect
              v-model="dataForm.permissionType"
              :type-code="'PERMISSION_TYPE'"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="email"
            prop="email"
          >
            <el-input
              v-model="dataForm.email"
              clearable
              placeholder="email"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row
        type="flex"
        class="row-bg"
      >
        <el-col :span="12">
          <el-form-item
            label="所属单位"
            prop="deptId"
          >
            <commonSelect
              v-model="dataForm.deptId"
              :type-code="'DEPT_NAME'"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="用户类型"
            prop="userType"
          >
            <commonSelect
              v-model="dataForm.userType"
              :type-code="'USER_TYPE'"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!--
      <el-row type="flex" class="row-bg">
        <el-col :span="12">
          <el-form-item label="性别" prop="sex">
            <commonSelect :typeCode="'SEX'" v-model="dataForm.sex"></commonSelect>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" class="row-bg">
        <el-col :span="12">
          <el-form-item label="办公电话" prop="tel">
            <el-input clearable v-model="dataForm.tel" placeholder="办公电话"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="传真" prop="fax">
            <el-input clearable v-model="dataForm.fax" placeholder="传真"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" class="row-bg">
        <el-col :span="12">
          <el-form-item label="联系手机" prop="mobile">
            <el-input clearable v-model="dataForm.mobile" placeholder="联系手机"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="email" prop="email">
            <el-input clearable v-model="dataForm.email" placeholder="email"></el-input>
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-form-item
        label="机构权限"
        size="mini"
        prop="deptIdList"
      >
        <el-checkbox-group v-model="dataForm.deptIdList">
          <el-checkbox
            v-for="dept in deptList"
            :key="dept.deptCode"
            :label="dept.deptCode"
          >
            {{ dept.deptName }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item
        label="角色"
        size="mini"
        prop="roleIdList"
      >
        <el-checkbox-group v-model="dataForm.roleIdList">
          <el-checkbox
            v-for="role in roleList"
            :key="role.roleId"
            :label="role.roleId"
          >
            {{ role.roleName }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item
        label="状态"
        size="mini"
        prop="status"
      >
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">
            禁用
          </el-radio>
          <el-radio :label="1">
            正常
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- <el-row
        type="flex"
        class="row-bg"
      >
        <el-col :span="12">
          <el-form-item
            label="照片"
            prop="photo"
          >
            <el-input
              v-model="dataForm.photo"
              clearable
              placeholder="照片"
              type="textarea"
              :autosize="{ minRows: 3, maxRows: 5}"
            />
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-row
        :gutter="20"
        style="margin"
      >
        <el-form-item
          v-if="imageList.length > 0"
          label="学生头像"
        >
          <el-col :span="24">
            <div>
              <uploadImageDetail
                :image-list="imageList"
                :link-table="dataForm.linkTable"
                :link-id="dataForm.linkId"
                :link-field="dataForm.linkField"
              />
            </div>
          </el-col>
        </el-form-item>
      </el-row>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        size="mini"
        @click="visible = false"
      >取消</el-button>
      <el-button
        size="mini"
        type="primary"
        :loading="btnLoading"
        @click="dataFormSubmit()"
      >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  // isMobile
  // import { isEmail } from '@/utils/validate'
  import commonSelect from '@/components/common-select'
  import { getAttachmentList } from '@/utils/common'
  import uploadImageDetail from '@/components/upload-image-detail'
  export default {
    components: { commonSelect, uploadImageDetail },
    data () {
      // var validatePassword = (rule, value, callback) => {
      //   if (!this.dataForm.userId && !/\S/.test(value)) {
      //     callback(new Error('密码不能为空'))
      //   } else {
      //     callback()
      //   }
      // }
      // var validateComfirmPassword = (rule, value, callback) => {
      //   if (!this.dataForm.userId && !/\S/.test(value)) {
      //     callback(new Error('确认密码不能为空'))
      //   } else if (this.dataForm.password !== value) {
      //     callback(new Error('确认密码与密码输入不一致'))
      //   } else {
      //     callback()
      //   }
      // }
      // var validateEmail = (rule, value, callback) => {
      //   if (!isEmail(value)) {
      //     callback(new Error('邮箱格式错误'))
      //   } else {
      //     callback()
      //   }
      // }
      // var validateMobile = (rule, value, callback) => {
      //   if (!isMobile(value)) {
      //     callback(new Error('手机号格式错误'))
      //   } else {
      //     callback()
      //   }
      // }
      return {
        visible: false,
        btnLoading: false,
        roleList: [],
        deptList: [],
        imageList: [],
        dataForm: {
          userId: '',
          userName: '',
          account: '',
          deptId: '',
          permissionType: '',
          password: '',
          comfirmPassword: '',
          salt: '',
          userType: '',
          sex: '',
          tel: '',
          fax: '',
          mobile: '',
          email: '',
          photo: '',
          remark: '',
          roleIdList: [],
          deptIdList: [],
          status: 1,
          linkTable: 'stu_student',
          linkId: '',
          linkField: 'studentPhoto',
        },
        dataRule: {
          account: [
            { required: true, message: '账号不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ]
          // password: [
          //   { validator: validatePassword, trigger: 'blur' }
          // ],
          // comfirmPassword: [
          //   { validator: validateComfirmPassword, trigger: 'blur' }
          // ],
          // email: [
            // { required: true, message: '邮箱不能为空', trigger: 'blur' },
            // { validator: validateEmail, trigger: 'blur' }
          // ]
          // mobile: [
          //   { required: true, message: '手机号不能为空', trigger: 'blur' },
          //   { validator: validateMobile, trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (userId) {
        this.dataForm.userId = userId || 0
        this.imageList = []
        getAttachmentList(this.dataForm.linkTable, this.dataForm.userId, this.dataForm.linkField).then(({data}) => {
          data.listMap.forEach(item => {
            this.imageList.push({'url': this.$http.requestUrl('/common/attachment/showFile/' + item.id + '?token=' + `${this.$cookie.get('token')}`), 'name': item.name, 'id': item.id})
          })
        })
        this.$http({
          url: this.$http.requestUrl('/sys/role/select'),
          method: 'get',
          params: this.$http.requestParams()
        }).then(({data}) => {
          this.roleList = data && data.code === 0 ? data.list : []
          this.deptList = data && data.code === 0 ? data.deptList : []
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.requestUrl(`/sys/user/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.requestParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.info.userName
                this.dataForm.account = data.info.account
                this.dataForm.deptId = data.info.deptId
                this.dataForm.permissionType = data.info.permissionType
                this.dataForm.salt = data.info.salt
                this.dataForm.status = data.info.status
                this.dataForm.userType = data.info.userType
                this.dataForm.sex = data.info.sex
                this.dataForm.tel = data.info.tel
                this.dataForm.fax = data.info.fax
                this.dataForm.mobile = data.info.mobile
                this.dataForm.email = data.info.email
                this.dataForm.photo = data.info.photo
                this.dataForm.remark = data.info.remark
                this.dataForm.roleIdList = data.info.roleIdList
                this.dataForm.deptIdList = data.info.deptIdList
                this.dataForm.status = data.info.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.btnLoading = true
            this.$http({
              url: this.$http.requestUrl(`/sys/user/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.requestData({
                'userId': this.dataForm.userId || undefined,
                'userName': this.dataForm.userName,
                'account': this.dataForm.account,
                'deptId': this.dataForm.deptId,
                'permissionType': this.dataForm.permissionType,
                'password': this.dataForm.password,
                'salt': this.dataForm.salt,
                'userType': this.dataForm.userType,
                'sex': this.dataForm.sex,
                'tel': this.dataForm.tel,
                'fax': this.dataForm.fax,
                'mobile': this.dataForm.mobile,
                'email': this.dataForm.email,
                'photo': this.dataForm.photo,
                'remark': this.dataForm.remark,
                'status': this.dataForm.status,
                'roleIdList': this.dataForm.roleIdList,
                'deptIdList': this.dataForm.deptIdList
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
              this.btnLoading = false
            })
          }
        })
      }
    }
  }
</script>
